package com.dongdong.bookstore.module.common.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.common.dto.SliceFile;
import com.dongdong.bookstore.module.common.service.UploadService;
import com.dongdong.bookstore.utils.FastDFSClient;
import com.github.tobato.fastdfs.domain.StorePath;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chiangkai
 * @date 2021-11-04
 * @email chiangkk@163.com
 */
@Api(tags = "文件上传")
@RestController
public class Upload {

    @Autowired
    private FastDFSClient fastDFSClient;

    @Autowired
    private UploadService uploadService;

    @ApiOperation(value = "头像上传")
    @PostMapping("/uploadImg")
    public Map<String, Object> headImgUpload(MultipartFile file) throws IOException {
        String path = "";
        // 开始文件上传
        if (file != null) {
            // 获得文件上传的文件名称
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotBlank(fileName)) {
                String suffix = FastDFSClient.getExtension(fileName).toLowerCase();
                List<String> exts = Arrays.asList("jpg", "png", "jpeg");
                if (!exts.contains(suffix)) {
                    Asserts.fail("图片格式不正确");
                }
                Map<String, Object> map = new HashMap<>();
                map.put("url", fastDFSClient.getWebServerUrl());
                map.put("storePath", fastDFSClient.uploadFileWithThumbImg(file));
                return map;
            }
        }
        Asserts.fail("上传失败");
        return null;
    }

    @SneakyThrows
    @GetMapping("/checkBigFile")
    public Map<String, Object> checkBigFile(String fileName, String fileMd5, Integer fileSize, Integer chunkSize) {
        return uploadService.checkBigFile(fileName, fileMd5, fileSize, chunkSize);
    }

    @SneakyThrows
    @PostMapping("/upload")
    public Map<String, Object> uploadFile(SliceFile sliceFile) {
        return uploadService.uploadSlice(sliceFile);
    }

    @SneakyThrows
    @PostMapping("/mergeFile")
    public Map<String, Object> mergeFile(@Param("fileName") String fileName,
                                         @Param("fileHash") String fileHash,
                                         @Param("chunkSize") Integer chunkSize,
                                         @Param("fileSize") Integer fileSize) {
        return uploadService.merge(fileName, fileHash, chunkSize, fileSize);
    }


}
